//
//  DetailSandTableViewController.m
//  YIJU2.0-HD
//
//  Created by 陈柏兵 on 16/5/6.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "DetailSandTableViewController.h"
#import "SettingSandTableLampView.h"
#import "DisplayComputerCollectionCell.h"
#import "SandLampCollectionCell.h"
#import "SandDetailCollectionHeaderView.h"
#import "Macro.h"
#import "Masonry.h"
#import "ComputerMessageViewController.h"
#import "LampMessageViewController.h"

#define kSandSectionOneTitle @"显示电脑设置"
#define kSandSectionTwoTitle @"沙盘灯光控制设置"

#define kIdentifierOne    @"ComputerCell" //电脑设置identifier
#define kIdentifierTwo    @"LampCell" //灯光控制设置identifier
#define kIdentifierThree  @"headerView" //头部view的identifier

@interface DetailSandTableViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) SettingSandTableLampView *LampView;//沙盘灯光控制设置
@property (nonatomic, strong) UICollectionView *collectionView;
@end

static NSString *kIdentifier = @"CollectionCell";

@implementation DetailSandTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"沙盘详情";
    self.view.backgroundColor = kSettingBackgroundColor;
    self.view.layer.masksToBounds = YES;
    [self layOutUI];
    
}

#pragma mark - UICollectionViewDelegate
//设置分组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

//设置每组item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    } else {
        return 20;
    }
}

//设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/10, SCREEN_WIDTH/10);
}

//每个item之间的间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.f;
}

//设置内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DisplayComputerCollectionCell *DCell = [collectionView dequeueReusableCellWithReuseIdentifier:kIdentifierOne forIndexPath:indexPath];
        DCell.computerImage.image = [UIImage imageNamed:@"BusinessTaIk"];
        DCell.titleLabel.text = @"你猜";
        
        return DCell;
    } else {
        SandLampCollectionCell *SCell = [collectionView dequeueReusableCellWithReuseIdentifier:kIdentifierTwo forIndexPath:indexPath];
        SCell.titleString = @"哈哈哈";
        
        
        return SCell;
    }
}

//设置section的header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 从重用队列里面获取
    SandDetailCollectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kIdentifierThree forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        header.SectionTitle = kSandSectionOneTitle;
    } else {
        header.SectionTitle = kSandSectionTwoTitle;
    }
    // 设置背景颜色
    header.backgroundColor = [UIColor clearColor];
    
    return header;
}

//设置header的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreenWidth, 40);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(0, SCREEN_WIDTH/15, 0, SCREEN_WIDTH/15);
    } else {
        return UIEdgeInsetsMake(0, 5, 0, 5);
    }
}

//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    DDLogInfo(@"item%ld点击了",indexPath.item);
    if (indexPath.section == 0) {
        ComputerMessageViewController *computer = [ComputerMessageViewController new];
        [self.navigationController pushViewController:computer animated:NO];
    } else {
        LampMessageViewController *lamp = [LampMessageViewController new];
        [self.navigationController pushViewController:lamp animated:NO];
    }
}

#pragma mark - private
- (void)layOutUI {
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left).offset(TweentyOffsetViewMargin);
        make.right.equalTo(self.view.mas_right).offset(-TweentyOffsetViewMargin);
        make.height.equalTo(self.view.mas_height);
    }];
}

#pragma mark - Getters
- (SettingSandTableLampView *)LampView {
    if (_LampView == nil) {
        _LampView = [[SettingSandTableLampView alloc] init];
        _LampView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_LampView];
    }
    return _LampView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout  *flowLayout =[[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[DisplayComputerCollectionCell class] forCellWithReuseIdentifier:kIdentifierOne];
        [_collectionView registerClass:[SandLampCollectionCell class] forCellWithReuseIdentifier:kIdentifierTwo];
        [_collectionView registerClass:[SandDetailCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kIdentifierThree];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

@end
